package cn.wqb.bpp.common.utils;

import java.security.MessageDigest;

public class SHA256Utils {

    public static String getSHA256(String content){
        MessageDigest messageDigest;
        try{
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(content.getBytes("UTF-8"));
            return byte2Hex(messageDigest.digest());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private static String byte2Hex(byte[] bytes){
        StringBuilder builder = new StringBuilder();
        String temp;
        for (int i=0;i<bytes.length;i++){
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if(temp.length() == 1){
                builder.append("0");
            }
            builder.append(temp);
        }
        return builder.toString();
    }

}
